import { UserIndexBatchComponent } from './user-index-batch/user-index-batch.component';
import { UserIndexEditComponent } from './user-index-edit/user-index-edit.component';
import { AbsTableUtil } from '@abstract/table';
import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { AuthService } from '@service/auth.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { IUser } from '@model/user';
import { UserService } from '@service/user.service';

@Component({
	selector: 'app-user-index',
	templateUrl: './user-index.component.html',
	styleUrls: ['./user-index.component.less']
})
export class UserIndexComponent extends AbsTableUtil implements OnInit {

	nzLoading = false;
	data = [];
	expand = false;
	warehouseNodes = [];
	tableScrollHeight= "calc(100vh - 180px)";
	toolTypeList = [];
	constructor(
		private userService: UserService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private authService: AuthService,
		private nzModalService: NzModalService

	) {
		super();
		this.authList = this.authService.getAuth();
	}

	ngOnInit() {
		this.buildForm();
		this.onSearch();
	}

	buildForm() {
		this.validateForm = this.fb.group({
			userCode: [''],
			userName: [''],
			warehouseId: [''],
			forbidden: [''],
			typeIdList: [[]]
		});
	}

	private changeWarehouse(toolTypeOpt: any[]) {
		toolTypeOpt.forEach(it => {
			it.isLeaf = !it.hasChildrenFlag;
			it.value = it.id;
			it.title = it.name;
			it.key = it.id;
			if (it?.children?.length) {
				this.changeWarehouse(it.children);
			}
		})
	}

	async onSearch(isInitPage?: boolean) {
		if (isInitPage) {
			this.page.page = 1;
		}
		this.nzLoading = true;
		const formValue = this.validateForm.value;
		// ['firstInspectDate', 'latestInspectDate', 'nextInspectDate'].forEach(key => {
		// 	if (formValue[key]?.length) {
		// 		formValue[key + 'Form'] = this.datePipe.transform(formValue[key][0], 'yyyy-MM-dd');
		// 		formValue[key + 'To'] = this.datePipe.transform(formValue[key][1], 'yyyy-MM-dd');
		// 	}
		// });
		formValue.forbidden = formValue.forbidden === null ? '' : formValue.forbidden;
		const result = await this.userService.getUserList({ ...this.page, ...formValue });
		this.nzLoading = false;
		this.data = result.content;
		this.calcTableHeight();
		this.page.total = result.totalElements;
	}
	create(item?: IUser) {
		this.nzModalService.create({
			nzTitle: item ? '修改' : '新增',
			nzContent: UserIndexEditComponent,
			nzWidth: '80%',
			nzStyle: {
				top: '30px'
			},
			nzComponentParams: {
				item: item
			},
			nzFooter: [
				{
					label: '取消',
					onClick: componentInstance => {
						componentInstance.destroyModal();
					}
				},
				{
					label: '确定',
					type: 'primary',
					autoLoading: true,
					onClick: async (componentInstance) => {
						const resForm = componentInstance.onSubmit() as any;
						if (!resForm) {
							return;
						}
						const result = !item ? await this.userService.addUser(resForm) : await this.userService.modifyUser({ ...item, ...resForm });
						this.nzMessageService.success('保存成功');
						!item ? this.onSearch(true) : this.onSearch();
						componentInstance.destroyModal();
					}
				}
			]
		});
	}
	batch() {
		this.nzModalService.create({
			nzTitle: '批量修改',
			nzContent: UserIndexBatchComponent,
			nzWidth: '80%',
			nzStyle: {
				top: '30px'
			},
			nzFooter: [
				{
					label: '取消',
					onClick: componentInstance => {
						componentInstance.destroyModal();
					}
				},
				{
					label: '确定',
					type: 'primary',
					autoLoading: true,
					onClick: async (componentInstance) => {
						const resForm = componentInstance.onSubmit() as any;
						if (!resForm) {
							return;
						}
						const result =  await this.userService.modifyBatchUser(resForm);
						this.nzMessageService.success('批量修改成功');
						this.onSearch(true);
						componentInstance.destroyModal();
					}
				}
			]
		});
	}

	calcTableHeight() {
		setTimeout(() => {
			const _thead = (document.querySelector('.table-container thead') as any);
			const _tableTheadHeight = _thead.offsetHeight;
			const _form = document.querySelector('form.page-header') as any;
			const _formHeight = _form.offsetHeight
			this.tableScrollHeight = `calc(100vh - ${40 + 50 + _tableTheadHeight + _formHeight}px)`;
		}, 200);
	}
}
